package com.nkzy.test;

import com.nkzy.mapper.UserMapper;
import com.nkzy.pojo.User; // 确保导入正确的 User 类
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

@SpringBootTest
public class UserMapperTest {

    @Mock
    private SqlSession sqlSession;

    @InjectMocks
    private UserMapper userMapper;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    public void testSelectUserById() {
        // 创建 User 对象并设置所有字段
        User expectedUser = new User();
        expectedUser.setId(1L);
        expectedUser.setUsername("John Doe");
        expectedUser.setPassword("password");
        expectedUser.setPhone("1234567890");
        expectedUser.setInfo("{\"key\": \"value\"}"); // 修改为 JSON 格式
        expectedUser.setStatus(1);
        expectedUser.setBalance(100);
        expectedUser.setCreateTime(new Date()); // 修改为 new Date()
        expectedUser.setUpdateTime(new Date()); // 修改为 new Date()

        when(sqlSession.selectOne("com.nkzy.mapper.UserMapper.selectUserById", 1)).thenReturn(expectedUser);

        User actualUser = userMapper.selectUserById();
        assertEquals(expectedUser, actualUser);
    }

    @Test
    public void testInsertUser() {
        User user = new User();
        user.setId(1L);
        user.setUsername("John Doe");
        user.setPassword("password");
        user.setPhone("1234567890");
        user.setInfo("{\"key\": \"value\"}"); // 修改为 JSON 格式
        user.setStatus(1);
        user.setBalance(100);
        user.setCreateTime(new Date()); // 修改为 new Date()
        user.setUpdateTime(new Date()); // 修改为 new Date()

        when(sqlSession.insert("com.nkzy.mapper.UserMapper.insertUser", user)).thenReturn(1);

        int result = userMapper.insertUser(user);
        assertEquals(1, result);
    }

    @Test
    public void testUpdateUser() {
        User user = new User();
        user.setId(2L);
        user.setUsername("John Doe");
        user.setPassword("password");
        user.setPhone("1234567890");
        user.setInfo("{\"key\": \"value\"}"); // 修改为 JSON 格式
        user.setStatus(1);
        user.setBalance(100);
        user.setCreateTime(new Date()); // 修改为 new Date()
        user.setUpdateTime(new Date()); // 修改为 new Date()
        when(sqlSession.update("com.nkzy.mapper.UserMapper.updateUser", user)).thenReturn(1);
        int result = userMapper.updateUser(user);
        assertEquals(1, result);
    }

    @Test
    public void testDeleteUser() {
        when(sqlSession.delete("com.nkzy.mapper.UserMapper.deleteUser", 1)).thenReturn(1);

        int result = userMapper.deleteUser();
        assertEquals(1, result);
    }

    @Test
    public void testSelectAllUsers() {
        // 创建 User 对象并设置所有字段
        User user1 = new User();
        user1.setId(1L);
        user1.setUsername("John Doe");
        user1.setPassword("password");
        user1.setPhone("1234567890");
        user1.setInfo("{\"key\": \"value\"}"); // 修改为 JSON 格式
        user1.setStatus(1);
        user1.setBalance(100);
        user1.setCreateTime(new Date()); // 修改为 new Date()
        user1.setUpdateTime(new Date()); // 修改为 new Date()

        User user2 = new User();
        user2.setId(2L);
        user2.setUsername("Jane Doe");
        user2.setPassword("password");
        user2.setPhone("0987654321");
        user2.setInfo("{\"key\": \"value\"}"); // 修改为 JSON 格式
        user2.setStatus(1);
        user2.setBalance(200);
        user2.setCreateTime(new Date()); // 修改为 new Date()
        user2.setUpdateTime(new Date()); // 修改为 new Date()

        List<User> expectedUsers = new ArrayList<>();
        expectedUsers.add(user1);
        expectedUsers.add(user2);

        when(sqlSession.selectList("com.nkzy.mapper.UserMapper.selectAllUsers")).thenReturn(expectedUsers);

        List<User> actualUsers = userMapper.selectAllUsers();
        assertEquals(expectedUsers, actualUsers);
    }
}